/**
 * Created by Administrator on 2016/12/15.
 */
!function () {
    var getUrlSearch = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r   = window.location.search.substr(1).match(reg);
        if (r != null) {
            return decodeURIComponent(r[2]);
        } else {
            return undefined;
        }
    }

    var param   = window.location.href.split('?')[1].split('=')[1],
        paramType = window.location.href.split('?')[1].split('=')[0],
        selText = decodeURIComponent(param);

    // 判断是分类列表进来还是通过搜索进来
    if(paramType == 'bookName') {
        var bookType = selText;
    } else {
        $('.header-seach').find('.keyword-group').find('.input-control').val(selText);
    }

    $('.header-seach').find('.keytype-group').find('.select-control option[value='+ paramType +']').attr("selected","selected")

    // 行业分类入口 数据实例
    var vocationVm = new Vue({
        el  : '#vocation_vm',
        data: {
            toggle  : 1,    // 显示项 1.显示正在加载 2.显示数据 3显示暂无数据
            bookType: bookType,
            infoList: []
        }
    });
    // 行业分类入口请求
    $.ajax({
        url     : '/api/gz/lg/vocation/vocationList.v',
        type    : "POST",
        dataType: "json",
        data    : {
            pageRowNum: 99999,
            pageIndex : 1
        },
        error   : function () {
            vocationVm.toggle = 3;
        }
    }).done(function (data) {
        if (data.status == '200') {
            if (data.data.dataList.length > 0) {
                vocationVm.toggle   = 2;
                vocationVm.infoList = data.data.dataList;
            } else {
                vocationVm.toggle = 3;
            }
        } else {
            vocationVm.toggle = 3;
        }
    });

    // 搜索列表 数据实例
    var searchListVm = new Vue({
        el  : '#searchList_vm',
        data: {
            toggle  : 1,    // 显示项 1.显示正在加载 2.显示数据 3显示暂无数据
            infoList: [],
            totalRow: 0
        }
    });

    var searchCustList = function (curr) {
        $.ajax({
            url     : '/api/gz/lg/cust/custList.v',
            type    : "POST",
            dataType: "json",
            data    : {
                status          : "",
                publishDateBegin: $('input[name="publishDateBegin"]').val() || '',
                publishDateEnd  : $('input[name="publishDateEnd"]').val() || '',
                regCapitalBegin : $('input[name="regCapitalBegin"]').val() || '',
                regCapitalEnd   : $('input[name="regCapitalEnd"]').val() || '',
                pageRowNum      : 4,
                pageIndex       : curr || 1,
                industry        : $('.hyfl-list').find('li.active').attr('data-industry') || '',
                params          : getUrlSearch('params') == undefined ? '' : getUrlSearch('params'),
                regNum          : '',
                credNum         : '',
                custName        : '',
                type            : 'oths'
            },
            error   : function () {
                searchListVm.toggle = 3;
            }
        }).done(function (data) {
            if (data.status == '200') {
                searchListVm.totalRow = data.data.pageObj.totalRow;
                if (data.data.dataList.length > 0) {
                    searchListVm.toggle   = 2;
                    searchListVm.infoList = data.data.dataList;
                    laypage({
                        cont : 'pagination_page', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
                        pages: data.data.pageObj.totalPage, //通过后台拿到的总页数
                        curr : curr || 1, //当前页
                        first: 1, //将首页显示为数字1,。若不显示，设置false即可
                        last : data.data.pageObj.totalPage, //将尾页显示为总页数。若不显示，设置false即可
                        skin : '#21b573',
                        jump : function (obj, first) { //触发分页后的回调
                            if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                                searchCustList(obj.curr);
                            }
                        }
                    });
                } else {
                    searchListVm.toggle = 3;
                }
            } else {
                searchListVm.toggle = 3;
            }
        });
    }

    searchCustList(1);

    // 搜索服务次数统计-添加纪录
    $.ajax({
        url     : '/api/gz/lg/serCount/add.v',
        type    : "POST",
        dataType: "json",
        data    : {
            selText:selText
        },
        error   : function () {
            console.log('搜索添加失败');
        }
    }).done(function (data) {
        if (data.status == '200') {
            console.log('搜索添加成功');
        } else {
            console.log('搜索添加失败');
        }
    });

    // 分类选择
    $('.hyfl-list').on('click', '.hyfl-link', function () {
        var $this = $(this);
        $this.parent().addClass('active').siblings().removeClass('active');
    });

    // 筛选
    $('#btn_search_filter').on('click', function () {
        var $this = $(this);
        var chinese = /^[\u4E00-\u9FFF]+$/,
            regCapitalBegin = $('[name="regCapitalBegin"]').val(),
            regCapitalEnd = $('[name="regCapitalEnd"]').val();
        if(chinese.test(regCapitalBegin) || chinese.test(regCapitalEnd)){
            layer.tips('注册资本值只能是数字', $this, {
                tips: [1, '#21b573']
            });
        } else {
            searchCustList(1);
        }
        return false;
    });
}();
